package com.clps.digital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.clps.digital.dao.FirstPublishDao;
import com.clps.digital.model.entity.FirstPublish;
import com.clps.digital.model.vo.FirstPublishVo;
import com.clps.digital.service.FirstPublishService;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Chase
 * @date: 2024/9/3 15:32
 * @Version: 1.0
 * @description:
 */

public class FirstPublishServiceImpl implements FirstPublishService {

    private FirstPublishDao firstPublishDao;

    /**
     * 获取startup首发列表
     * @return
     */
    @Override
    public List<FirstPublishVo> getFirstPublishList() {
        List<FirstPublish> firstPublishList = firstPublishDao.getFirstPublishList();
        BeanUtil.copyToList(firstPublishList, FirstPublishVo.class);
        List<FirstPublishVo> firstPublishVoList = firstPublishList.stream().map(firstPublish -> {
            FirstPublishVo firstPublishVo = new FirstPublishVo();
            BeanUtil.copyProperties(firstPublish, firstPublishVo);
            long timeDifference = firstPublish.getSoldTime().getTime() - LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            if (timeDifference <= 0) {
                firstPublishVo.setSoldTime("0天00:00:00");
                firstPublishVo.setIsProceed((short) 0);
            } else {
                firstPublishVo.setSoldTime(transitionSoldTime(timeDifference));
                firstPublishVo.setIsProceed((short) 1);
            }
            return firstPublishVo;
        }).collect(Collectors.toList());
        return firstPublishVoList;
    }

    private String transitionSoldTime(Long timeDifference) {
        // 将毫秒转换为秒
        long seconds = timeDifference / 1000;
        // 将秒转换为分钟
        long minutes = seconds / 60;
        // 计算小时和天数
        long hours = minutes / 60;
        long days = hours / 24;
        return String.format("%d天%02d:%02d:%02d", days, hours % 24, minutes % 60, seconds % 60);
    }
}
